package cn.healthlink.proxy.demo04;

import java.util.List;

/**
 * @author dengzhiyuan
 * @version 1.0
 * @time 2021/2/24 15:19
 */
public class MyMethodInvocationImpl implements MyMethodInvocation{

	private TargetParam targetParam;

	private List<MyMethodInterceptor> interceptorList;

	private int index = 0;

	@Override
	public Object proceed() throws Throwable{
		//拦截器全部执行结束
		if(index == interceptorList.size()){
			Object invoke = targetParam.getMethod().invoke(targetParam.getTarget(), targetParam.getArgs());
			System.out.println("结果："+invoke);
			return invoke;
		}
		//说明还有拦截器没有执行
		MyMethodInterceptor interceptor = interceptorList.get(index++);
		return interceptor.invoke(this);
	}

	public MyMethodInvocationImpl(TargetParam targetParam, List<MyMethodInterceptor> interceptorList) {
		this.targetParam = targetParam;
		this.interceptorList = interceptorList;
	}
}
